package org.hxzon.demo.jt.logsearcher.web.pages;

import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.ajax.AjaxResponseRenderer;
import org.hxzon.demo.jt.logsearcher.util.LogFileSearcher;
import org.hxzon.demo.jt.logsearcher.util.LogList;
import org.hxzon.demo.jt.logsearcher.util.LogSearcherConfig;

public class Index {

    @Inject
    private AjaxResponseRenderer ajaxResponseRenderer;
    @InjectComponent
    private Zone zone;
    //
    private LogFileSearcher searcher;
    //
    @Property
    private String analyzerName="cjk";
    @Property
    private List<String> indexNames;
    @Property
    private int resultCount = 100;
    @Property
    private String searchText;
    @Property
    private List<LogList> searchResult;

    @OnEvent(value = EventConstants.ACTIVATE)
    void init() {
        searcher = new LogFileSearcher(LogSearcherConfig.IndexFileBaseDir, analyzerName);
        indexNames = searcher.listIndexNames();
    }

    @OnEvent(value = EventConstants.SUBMIT)
    void search() {
        searcher = new LogFileSearcher(LogSearcherConfig.IndexFileBaseDir, analyzerName);
        searchResult = searcher.search(indexNames.toArray(new String[indexNames.size()]), //
                searchText, resultCount);
        ajaxResponseRenderer.addRender("zone", zone);
    }
}
